/**
 * 
 */
package model;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;

/**
 * @author kambo
 *	Writes textFiles
 */
public class ReadWriter {
	private BufferedReader inputStream = null;
	private PrintWriter outputStream = null;
	private String fileName;
	private String inputString;
	private PrintWriter logFileWriter;
	
	/**
	 * Constructor
	 * @param fileName
	 * @param inputString
	 */
	public ReadWriter(String fileName, String inputString) {
		this.fileName = fileName;
		this.inputString = inputString;
	}
	
	public ReadWriter(String fileName) {
		this.fileName = fileName;
	}
	
	/**
	 * Writes a file
	 * @throws IOException
	 */
	public void writeFile() throws IOException {
		try {
			inputStream = new BufferedReader(new StringReader(inputString));
			outputStream = new PrintWriter(new FileWriter(fileName));
			String l;
			
            while ((l = inputStream.readLine()) != null) {
                outputStream.println(l);}
			
		} finally {
			if (inputStream != null) {
				inputStream.close();
				
			if (outputStream != null) {
				outputStream.close();
			}
			}
		}
		
	}
	public PrintWriter getLogFileWriter() {
		return logFileWriter;
	}


	public void setLogFileWriter(PrintWriter logFileWriter) {
		this.logFileWriter = logFileWriter;
	}

	public void matchLogFile() {
		try {
	        logFileWriter = new PrintWriter(new FileWriter(fileName));
	      } catch (Exception e) {  
	        return;
	      }
		
	}

	public void close() {
		try {
	        logFileWriter.close();
	      } catch (Exception e) {
	      }
		
	}

	public void printToLog(String url) {
		try {
		    logFileWriter.println(url);
		  } catch (Exception e) {
		  }
		}
		
	}

	

